const app = getApp()
Component({
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  properties: {
    count: String, //父子传参
    className: String,
    modelName: String,
    cropper: Boolean
  },
  /**
   * 页面的初始数据
   */
  data: {
    imgs: [],
    count: 1, //限制图片数量
    ishide: false,
    imgUrl: app.globalData.imgUrl,
    uploadUrl: wx.getStorageSync('uploadUrl'),
    fileName: "",
    chooseImage:'',
    cropper: false
  },
  // 上传图片
  methods: {
    chooseImg: function(e) {
      var that = this;
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function(res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths[0]; 
          that.setData({
            chooseImage: res.tempFilePaths[0]
          })
          if (!that.data.cropper) {
            that.uploadFile(tempFilePaths);
          }
 
        }
      });
    },


    // 预览图片
    previewImg: function(e) {
      //获取当前图片的下标
      var index = e.currentTarget.dataset.index;

      //所有图片
      var imgs = this.data.datalist.map(item => {
        return this.data.hosts + item.imgPath
      });
      wx.previewImage({
        //当前显示图片
        current: imgs[index],
        //所有图片
        urls: imgs
      })
    },



    //获取裁剪图片
    getCropperImg(e) {
      var that = this;
      that.cropperUploadFile(e.detail.url);
    },


    cropperUploadFile: function(pics) {
      var that = this;
      that.setData({ 
        loading: true
      })
      wx.uploadFile({
        url: app.globalData.apiUrl + "/" + that.data.modelName + '/protected/comm/file/' + that.data.className + '/fileUpload',
        header: {
          "Authorization": wx.getStorageSync('Authorization'),
        },
        formData: {
          width: 750
        },
        filePath: pics,
        name: 'file',
        success: function(res) {
          if (typeof res.data == "string") {
            var resdata = JSON.parse(res.data)
          }
          if (resdata.success) {
            var picObj = {
              imgName: resdata.datas.vo.fileName,
              imgPath: resdata.datas.vo.relativeFilePath
            }
            
            that.setData({
              picslist: picObj,
              loading: false, 
              chooseImage: ''
            }) 
          }
        },
        fail: function(res) {
          wx.showToast({
            icon: 'none',
            title: '图片上传失败',
          })
        }

      })
    },




    //上传图片
    uploadFile: function(pics) {
      var that = this;
      that.setData({
        loading: true
      })
      wx.uploadFile({
        url: app.globalData.apiUrl + "/" + that.data.modelName + '/protected/comm/file/' + that.data.className + '/fileUpload',
        header: {
          "Authorization": wx.getStorageSync('Authorization'),
        },
        formData: {
          width: 750
        },
        filePath: pics,
        name: 'file',
        success: function(res) {
          if (typeof res.data == "string") {
            var resdata = JSON.parse(res.data)
          }
          if (resdata.success) {
            var picObj = {
              imgName: resdata.datas.vo.fileName,
              imgPath: resdata.datas.vo.relativeFilePath
            }
            that.setData({
              picslist: picObj,
              loading: false,
              chooseImage: ''
            })
          }
        },
        fail: function(res) {
          wx.showToast({
            icon: 'none',
            title: '图片上传失败',
          })
        }

      })
    },
  },


})